package generator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;

/**
 * 根据jar包生成pom依赖
 * 注意:<br>
 *     因为没用mybatis-plus的Service和Controller所以生成完之后要删掉TTT目录
 * @author szfzf
 * @Date 2017/6/28
 */
public class Jar2PomGenerator {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		Element dependencys = new DOMElement("dependencies");
		File dir = new File("c://Users//felix//posp//src//server//posp-boss//WebRoot//WEB-INF//lib//");
		for (File jar : dir.listFiles()) {
			JarInputStream jis = new JarInputStream(new FileInputStream(jar));
			Manifest mainmanifest = jis.getManifest();
			jis.close();
			System.out.println(jar.getName());
			String bundleName = mainmanifest == null ? null : mainmanifest.getMainAttributes().getValue("Bundle-Name");
			String bundleVersion =  mainmanifest == null ? null :mainmanifest.getMainAttributes().getValue("Bundle-Version");
			Element ele = null;
			

			StringBuffer sb = new StringBuffer(jar.getName());
			if (bundleName != null) {
				bundleName = bundleName.toLowerCase().replace(" ", "-");
				sb.append(bundleName+"\t").append(bundleVersion);
				ele = getDependices(bundleName, bundleVersion);
				System.out.println(sb.toString());
				System.out.println(ele.asXML());
			}
			if (ele == null || ele.elements().size() == 0) {
				bundleName = "";
				bundleVersion = "";
				String[] ns = jar.getName().replace(".jar", "").split("-");
				for (String s : ns) {
					if (Character.isDigit(s.charAt(0))) {
						bundleVersion += s + "-";
					} else {
						bundleName += s + "-";
					}
				}
				if (bundleVersion.endsWith("-")) {
					bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1);
				}
				if (bundleName.endsWith("-")) {
					bundleName = bundleName.substring(0, bundleName.length() - 1);
				}
				ele = getDependices(bundleName, bundleVersion);
				sb.setLength(0);
				sb.append(bundleName+"\t").append(bundleVersion);
				System.out.println(sb.toString());
				System.out.println(ele.asXML());
			}
			
			if ( ele.elements().size() == 0) {
				ele.add(new DOMElement("groupId").addText("not.find"));
				ele.add(new DOMElement("artifactId").addText(bundleName));
				ele.add(new DOMElement("version").addText("1.0"));
				//ele.add(new DOMElement("scope").addText("system"));
				//ele.add(new DOMElement("systemPath").addText("${project.basedir}/WebRoot/WEB-INF/lib/"+jar.getName()));
			}
			//总是使用本地jar包 
			ele.add(new DOMElement("scope").addText("system"));
			ele.add(new DOMElement("systemPath").addText("${project.basedir}/WebRoot/WEB-INF/lib/"+jar.getName()));
			
			dependencys.add(ele);
			System.out.println();
		}
		System.out.println(dependencys.asXML());
	}

	public static Element getDependices(String key, String ver) {
		Element dependency = new DOMElement("dependency");
		// 设置代理
		// System.setProperty("http.proxyHost", "127.0.0.1");
		// System.setProperty("http.proxyPort", "8090");
		try {
			//String url = "http://search.maven.org/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver + "%22&rows=3&wt=json";
			
			String url = "http://10.128.14.11:8081/nexus/service/local/lucene/search?g="+"&a="+key+"&v="+ver;
			System.out.println(url);			
			org.jsoup.nodes.Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(30000).get();
			org.jsoup.nodes.Element element = doc.getElementsByTag("artifact").first(); 
			//for (org.jsoup.nodes.Element element : elements2) {  
				Element groupId = new DOMElement("groupId");
				Element artifactId = new DOMElement("artifactId");
				Element version = new DOMElement("version");
				;
				groupId.addText(element.getElementsByTag("groupId").text());
				artifactId.addText(element.getElementsByTag("artifactId").text());
				version.addText(element.getElementsByTag("version").text());
				dependency.add(groupId);
				dependency.add(artifactId);
				dependency.add(version);				
			//}
			/*
			String elem = doc.body().text();
			JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
			if (response.containsKey("artifact") && response.getJSONArray("artifact").size() > 0) {
				JSONObject docJson = response.getJSONArray("artifact").getJSONObject(0);
				Element groupId = new DOMElement("groupId");
				Element artifactId = new DOMElement("artifactId");
				Element version = new DOMElement("version");
				groupId.addText(docJson.getString("groupId"));
				artifactId.addText(docJson.getString("artifactId"));
				version.addText(docJson.getString("version"));
				dependency.add(groupId);
				dependency.add(artifactId);
				dependency.add(version);
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dependency;
	}

}